﻿using System;
using LL.Common;

namespace LL.Business
{
    /// <summary>
    /// 通用实体服务
    /// </summary>
    public class GenericService2 : ServiceBase
    {
        public InsertResult Insert<T>(T entity)
            where T : class,new()
        {
            var result = new InsertResult();
            try
            {
                var rows = DbSession.Insert(entity);
                result.DataEffected = rows;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public BatchInsertResult BatchInsert<T>(params T[] list)
            where T : class,new()
        {
            var result = new BatchInsertResult();
            try
            {
                foreach (T entity in list)
                {
                    result.DataEffected += DbSession.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public DeleteResult Delete<T>(T entity)
            where T : class,new()
        {
            var result = new DeleteResult();
            try
            {
                var rows = DbSession.Delete(entity);
                result.DataEffected = rows;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public DeleteResult Delete<T>(params object[] keys)
            where T : class,new()
        {
            var result = new DeleteResult();
            try
            {
                var rows = DbSession.Delete<T>(keys);
                result.DataEffected = rows;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public BatchDeleteResult BatchDelete<T>(params T[] list)
            where T : class,new()
        {
            var result = new BatchDeleteResult();
            try
            {
                foreach (T entity in list)
                {
                    result.DataEffected += DbSession.Delete(entity);
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public UpdateResult Update<T>(T entity)
            where T : class,new()
        {
            var result = new UpdateResult();
            try
            {
                var rows = DbSession.Update(entity);
                result.DataEffected = rows;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public BatchUpdateResult BatchUpdate<T>(params T[] list)
            where T : class,new()
        {
            var result = new BatchUpdateResult();
            try
            {
                foreach (T entity in list)
                {
                    result.DataEffected += DbSession.Update(entity);
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public FetchResult<T> Fetch<T>(params object[] keys)
            where T : class,new()
        {
            var result = new FetchResult<T>();
            try
            {
                result.Data = DbSession.Get<T>(keys);
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return result;
        }

        public LoadDataResponse<T> Fetch<T>(LoadDataRequest request)
            where T : class,new()
        {
            return DbSession.Query<T>(request);
        }
    }
}